<?php
namespace App\Admin\Metrics\Examples;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Http\Request;

class Sessions extends Bar
{
    public function init()
    {
        parent::init();

        $color = Admin::color();

        $dark35 = $color->dark35();

        // 设置卡片内容宽度
        $this->contentWidth(2, 5);

        // 设置卡片高度
        $this->height(300);

        // 设置图表高度
        $this->chartHeight(100);
        $this->chartWidth(100);

        // 标题
        $this->title('订单统计');

        // 设置下拉选项
        $this->dropdown([
            '7' => 'Last 7 Days',
            '28' => 'Last 28 Days',
            '30' => 'Last Month',
            '365' => 'Last Year',
        ]);

        // 设置图表颜色
        $this->chartColors([
            $color->primary(),
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        switch ($request->get('option')) {
            case '12':
            default:
                $orderData = $this->fetchOrderDataForLast7Days();

                $totalOrders = array_sum($orderData);

                $this->withContent('订单统计', $totalOrders);

                $this->withChart([
                    [
                        'name' => 'Orders',
                        'data' => $orderData,
                    ],
                ]);
        }
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'categories' => [
                'Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5', 'Day 6', 'Day 7','Day 8','Day 9','Day 10','Day 11','Day 12'
            ],
            'series' => $data,
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $title
     * @param string $value
     *
     * @return $this
     */
    public function withContent($title, $value)
    {
        // Get the selected option label
        $label = strtolower(
            $this->dropdown[request()->option] ?? 'last 7 days'
        );

        $minHeight = '185px';

        return $this->content(
            <<<HTML
<!-- <div class="d-flex p-1 flex-column justify-content-between" style="padding-top: 0;width: 100%;height: 100%;min-height: {$minHeight}">
    <div class="text-left">
        <h1 class="font-large-2 mt-2 mb-0">{$title}</h1>
        <h5 class="font-medium-2" style="margin-top: 10px;">
            <span class="text-success">{$value} </span>
            <span>vs {$label}</span>
        </h5>
    </div>
</div> -->
HTML
        );
    }

    /**
     * Fetch order data for the last 7 days (you may need to implement this logic)
     *
     * @return array
     */
    private function fetchOrderDataForLast7Days()
    {
        return [10, 15, 20, 25, 30, 35, 40,50,60,70,80,85];
    }
}
